//
//  TCPushDecorateView.m
//  TCLVBIM
//
//  Created by zhangxiang on 16/8/1.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "TCPushDecorateView.h"
#import "TCMsgBulletView.h"
#import "UIView+Additions.h"
#import "TCPusherModel.h"
#import "UIImage+Additions.h"
#import "UIView+CustomAutoLayout.h"
#import "TCConstants.h"
#import "TCLoginModel.h"
#import "V8HorizontalPickerView.h"
#import "MessageTableViewCell.h"

#import "privateChat.h"
//#import "MessageViewController.h"
#import "ConversationListViewController.h"


#if POD_PITU
#import "MCCameraDynamicView.h"

@interface TCPushDecorateView () <MCCameraDynamicDelegate>

@end
#endif

@implementation TCPushDecorateView
{
    TCShowLiveTopView     *_topView;
    TCPushShowResultView  *_resultView;
    TCAudienceListTableView *_audienceTableView;
    TCMsgListTableView    *_msgTableView;
    TCMsgBulletView *_bulletViewOne;
    TCMsgBulletView *_bulletViewTwo;
    AVIMMsgHandler        *_msgHandler;
    TCLiveInfo            *_liveInfo;
    UIView                *_msgInputView;
    UITextField           *_msgInputFeild;
    UIButton              *_closeBtn;
    CGPoint               _touchBeginLocation;
    CGPoint               _touchPrevLocation;
    BOOL                  _bulletBtnIsOn;
    
    UILabel                 *_xiubi;
    
    UIAlertView           *_closeAlert;
    UIAlertView           *_closeErrAlert;
    UIAlertView           *_closeErrRstAlert;

    UIButton              *_beautyBtn;
    UIButton              *_filterBtn;
    
    UILabel               *_beautyLabel;
    UILabel               *_whiteLabel;
    UILabel               *_bigEyeLabel;
    UILabel               *_slimFaceLabel;
    
    UISlider              *_sdBeauty;
    UISlider              *_sdWhitening;
    UISlider              *_sdBigEye;
    UISlider              *_sdSlimFace;
    
    UILabel               *_labAudioEffect;
    UILabel               *_labVolumeForVoice;
    UISlider              *_sldVolumeForVoice;
    UILabel               *_labVolumeForBGM;
    UISlider              *_sldVolumeForBGM;
    
    UIButton              *_btnSelectBGM;
    UIButton              *_btnStopBGM;
    
    UIView                *_vBGMPanel;
    UIView                *_vAudioEffectPanel;
    
    NSMutableArray*         _audioEffectArry;
    NSMutableArray*         _audioEffectViewArry;
    NSInteger              _audioEffectSelectedType;
    
    BOOL                  _isTouchMusicPanel;
    
    BOOL                  _viewsHidden;
    NSMutableArray        *_heartAnimationPoints;
	
    UIButton              *_motionBtn;
#if POD_PITU
    MCCameraDynamicView   *_tmplBar;
#else
    UIView                *_tmplBar;
#endif
    UIButton              *_greenBtn;
    V8HorizontalPickerView  *_greenPickerView;
    NSMutableArray *_greenArray;
    
    int    _filterType;
    int    _greenIndex;;
    V8HorizontalPickerView  *_filterPickerView;
    NSMutableArray *_filterArray;
    
    UITapGestureRecognizer *_tap;
    
    
    NSString            *_receiveInfo; // 私聊对象信息
    UITableView         *_tableView; // 私聊列表
    UITableView         *_tableView2;// 私聊对话列表
    UIView              *_textBgView;// 私聊底部对话框
    UITextField         *_privateChatTextF; // 私聊输入框
    TIMConversation     *_currConversation; // 当前会话
    
    UIView              *_msgView;
    
    
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLogout:) name:logoutNotification object:nil];
        
        //借这个tap手势跳到delegate调用原来声网的切换大小窗方法;
        _tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doDoubleTapped:)];
        //[self addGestureRecognizer:_tap];

        _audioEffectArry = [NSMutableArray arrayWithObjects:@"原声", @"KTV", @"房间", @"会堂", @"低沉", @"洪亮", @"金属", @"磁性", nil];
        _audioEffectViewArry = [NSMutableArray arrayWithCapacity:8];
        
        _audioEffectSelectedType = 0;
        
       [self initUI];
        
        // 注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendBannedMsg:)name:@"sendBannedMsg0" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendRemoveAudienceMsg:)name:@"sendRemoveAudienceMsg0" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendSetAdministratorMsg:) name:@"sendSetAdministratorMsg" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendDeleteAdministratorMsg:) name:@"sendDeleteAdministratorMsg" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendPrivateChatMsg:)name:@"sendPrivateChatMsg0" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(c2cNotification:)name:@"c2cNotification" object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendBannedMsg0" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendRemoveAudienceMsg0" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendSetAdministratorMsg" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendDeleteAdministratorMsg" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"sendPrivateChatMsg0" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"c2cNotification" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setPublishInfo:(TCPublishInfo *)publishInfo {
    _msgHandler = publishInfo.msgHandler;
    _msgHandler.roomIMListener = self;
    _liveInfo = publishInfo.liveInfo;
    
    //topview,展示主播头像，在线人数及点赞
    int statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
	
	//现在的逻辑是因为主播一定是商家，商家就要+V，所以直接给2（目前沿用之前项目呢逻辑，2是V，3是“专”字）
    _topView = [[TCShowLiveTopView alloc] initWithFrame:CGRectMake(5, statusBarHeight + 5, 100, 35) isHost:YES hostNickName:_liveInfo.userinfo.nickname
                                          audienceCount:_liveInfo.viewercount likeCount:_liveInfo.likecount hostFaceUrl:_liveInfo.userinfo.headpic isVIP:@"2" isLike:_liveInfo.isLike hostId:_liveInfo.myuserid roomId:_liveInfo.groupid];
    [self addSubview:_topView];
    [_topView startLive];
    
    //秀币
    UILabel *showCoin = [[UILabel alloc] init];
    //                         WithFrame:CGRectMake(_topView.left, _topView.bottom + 10, 60, 20)];
    showCoin.textColor = [UIColor whiteColor];
    [showCoin setFrame:CGRectMake(_topView.left, _topView.bottom + 10, 50, 20)];
    [self addSubview:showCoin];
    showCoin.backgroundColor = RGBA(50, 50, 50, 0.5);
    showCoin.font = kUHSystemFontWithSize(10);
    showCoin.text = [NSString stringWithFormat:@"秀币 %@个", _liveInfo.money];
//    NSMutableAttributedString *hintString = [[NSMutableAttributedString alloc] initWithString:showCoin.text];
    //获取要调整颜色的文字位置,调整颜色
//    NSRange range1=[[hintString string]rangeOfString:@"秀币"];
//    [hintString addAttribute:NSForegroundColorAttributeName value:RGB(225, 181, 52) range:range1];
//    
//    showCoin.attributedText = hintString;
    CGSize coinSize = [LZCTools textSizeWithText:showCoin.text font:kUHSystemFontWithSize(10)];
    showCoin.frame = CGRectMake(_topView.left, _topView.bottom+10, coinSize.width+SW(6), coinSize.height+5);
    showCoin.layer.cornerRadius = (coinSize.height+5)/2;
    showCoin.layer.masksToBounds = YES;
    showCoin.textAlignment = NSTextAlignmentCenter;
    
    _xiubi = showCoin;
    
    
    // 水印
    UIButton *watermark = [UIButton buttonWithType:UIButtonTypeCustom];
    //[self addSubview:watermark];
    [watermark setImage:IMAGE(@"watermark") forState:UIControlStateNormal];
    watermark.userInteractionEnabled = NO;
    watermark.frame = CGRectMake(self.width-_topView.left-SW(4), _topView.bottom+10, SW(4), SW(8));
    
    // 贡献榜
    
    UILabel *contribution = [[UILabel alloc] init];
    contribution.textColor = [UIColor whiteColor];
    [self addSubview:contribution];
    contribution.backgroundColor = RGBA(50, 50, 50, 0.5);
    contribution.font = kUHSystemFontWithSize(10);
    contribution.text = @"  贡献榜 2345 >";
    // 创建一个富文本
    NSMutableAttributedString *hintString2 = [[NSMutableAttributedString alloc] initWithString:contribution.text];
    //获取要调整颜色的文字位置,调整颜色
    NSRange range2=[[hintString2 string]rangeOfString:@"贡献榜"];
    [hintString2 addAttribute:NSForegroundColorAttributeName value:RGB(225, 181, 52) range:range2];
    // 添加表情
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    attch.image = [UIImage imageNamed:@"live_gxb"];
    // 设置图片大小
    attch.bounds = CGRectMake(2.5, -2.5, coinSize.height, coinSize.height);
    // 创建带有图片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithAttributedString:string];
    [string2 appendAttributedString:hintString2];
    
    // 用label的attributedText属性来使用富文本
    contribution.attributedText = string2;
    CGSize contributionSize = [LZCTools textSizeWithText:contribution.text font:kUHSystemFontWithSize(10)];
    
    contribution.frame = CGRectMake(self.width-_topView.left-(contributionSize.width+15), _topView.bottom+10, contributionSize.width+15, contributionSize.height+5);
    contribution.layer.cornerRadius = contribution.height/2;
    contribution.layer.masksToBounds = YES;
    contribution.textAlignment = NSTextAlignmentCenter;
    [contribution addTarget:self action:@selector(contributionClick:)];
    
    contribution.hidden = YES;
    
    //观众列表
    CGFloat audience_width = self.width - 25 - _topView.right;
    _audienceTableView = [[TCAudienceListTableView alloc] initWithFrame:CGRectMake(_topView.right + 10 +audience_width / 2 - IMAGE_SIZE / 2 ,_topView.center.y -  audience_width / 2, _topView.height, audience_width) style:UITableViewStyleGrouped liveInfo:_liveInfo];
    _audienceTableView.transform = CGAffineTransformMakeRotation(- M_PI/2);
	_audienceTableView.audienceListDelegate = self;
	NSLog(@"livefo is %@", _liveInfo);
    [self addSubview:_audienceTableView];
}

-(void) initUI
{
    int   icon_size = BOTTOM_BTN_ICON_WIDTH;
    float startSpace = 15;
    float icon_count = 7;
    float icon_center_y = self.height - icon_size/2 - startSpace;
    float icon_center_interval = (self.width - 2*startSpace - icon_size)/(icon_count - 1);
    float first_icon_center_x = startSpace + icon_size/2;
    _greenArray = [NSMutableArray new];
    [_greenArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"无";
        v.file = nil;
        v.face = [UIImage imageNamed:@"greens_no"];
        v;
    })];
    [_greenArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"卡通";
        v.file = [[NSBundle mainBundle] URLForResource:@"goodluck" withExtension:@"mp4"];;
        v.face = [UIImage imageNamed:@"greens_1"];
        v;
    })];
    [_greenArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"DJ";
        v.file = [[NSBundle mainBundle] URLForResource:@"2gei_5" withExtension:@"mp4"];
        v.face = [UIImage imageNamed:@"greens_2"];
        v;
    })];

    _filterArray = [NSMutableArray new];
    
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"原图";
        v.face = [UIImage imageNamed:@"orginal"];
        v;
    })];
    
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"美白";
        v.face = [UIImage imageNamed:@"fwhite"];
        v;
    })];
    
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"浪漫";
        v.face = [UIImage imageNamed:@"langman"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"清新";
        v.face = [UIImage imageNamed:@"qingxin"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"唯美";
        v.face = [UIImage imageNamed:@"weimei"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"粉嫩";
        v.face = [UIImage imageNamed:@"fennen"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"怀旧";
        v.face = [UIImage imageNamed:@"huaijiu"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"蓝调";
        v.face = [UIImage imageNamed:@"landiao"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"清凉";
        v.face = [UIImage imageNamed:@"qingliang"];
        v;
    })];
    [_filterArray addObject:({
        V8LabelNode *v = [V8LabelNode new];
        v.title = @"日系";
        v.face = [UIImage imageNamed:@"rixi"];
        v;
    })];
    
    
    //    _btnLog.center = CGPointMake(first_icon_center_x + icon_center_interval*6, icon_center_y-icon_size-10);
    // 弹出框的背景View
    _btnBgView = [[UIView alloc] init];
    [self addSubview:_btnBgView];
    _btnBgView.center = CGPointMake(first_icon_center_x + icon_center_interval*5, icon_center_y- (icon_size*5/2+SW(80)*3));
    _btnBgView.bounds = CGRectMake(0, 0, icon_size+SW(40), icon_size*3+SW(80)*4+10);
//    _btnBgView.backgroundColor = [UIColor whiteColor];
//    _btnBgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"live_btn_bg"]];
//    _btnBgView.alpha = 0.5;
    UIImage *image = [UIImage imageNamed:@"live_btn_bg"];
    _btnBgView.layer.contents = (id) image.CGImage;    // 如果需要背景透明加上下面这句
    _btnBgView.alpha = 0.5;
    _btnBgView.hidden = YES;
    
    
    //聊天
    _btnChat = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnChat.center = CGPointMake(first_icon_center_x, icon_center_y);
    _btnChat.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnChat setImage:[UIImage imageNamed:@"live_chat"] forState:UIControlStateNormal];
    [_btnChat addTarget:self action:@selector(clickChat:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnChat];
    
    //开启闪关灯按钮
    _btnTorch = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btnTorch.center = CGPointMake( first_icon_center_x + icon_center_interval, icon_center_y);
    _btnTorch.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnTorch setImage:[UIImage imageNamed:@"live_flash_close2"] forState:UIControlStateNormal];
    [_btnTorch addTarget:self action:@selector(clickTorch:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_btnTorch];
    [_btnBgView addSubview:_btnTorch];
    _btnTorch.center = CGPointMake(icon_size/2+SW(80), _btnBgView.height-icon_size*2-icon_size/2-SW(80)*3-SW(40));
    
    //前置后置摄像头切换
    _btnCamera = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btnCamera.center = CGPointMake(first_icon_center_x + icon_center_interval*2, icon_center_y);
    _btnCamera.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnCamera setImage:[UIImage imageNamed:@"live_camera2"] forState:UIControlStateNormal];
    [_btnCamera addTarget:self action:@selector(clickCamera:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_btnCamera];
    [_btnBgView addSubview:_btnCamera];
    _btnCamera.center = CGPointMake(icon_size/2+SW(80), _btnBgView.height-icon_size-icon_size/2-SW(80)*2-SW(40));
    
    //美颜开关按钮
    _btnBeauty = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnBeauty.center = CGPointMake(first_icon_center_x + icon_center_interval*4, icon_center_y);
    _btnBeauty.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnBeauty setImage:[UIImage imageNamed:@"live_beauty"] forState:UIControlStateNormal];
    [_btnBeauty addTarget:self action:@selector(clickBeauty:) forControlEvents:UIControlEventTouchUpInside];
    //[self addSubview:_btnBeauty];
    
    //音乐按钮
    _btnMusic = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btnMusic.center = CGPointMake(first_icon_center_x + icon_center_interval*4, icon_center_y);
    
    _btnMusic.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnMusic setImage:[UIImage imageNamed:@"live_music2"] forState:UIControlStateNormal];
    [_btnMusic addTarget:self action:@selector(clickMusic:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_btnMusic];
    [_btnBgView addSubview:_btnMusic];
    _btnMusic.center = CGPointMake(SW(80)+icon_size/2, _btnBgView.height-SW(80)-icon_size/2-SW(40));
    
    // 镜像
//    _isMirror = NO;
    _btnMirror = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnMirror.center = CGPointMake(icon_size/2+SW(80), _btnBgView.height-icon_size*3-icon_size/2-SW(80)*4-SW(40));
    _btnMirror.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnMirror setImage:[UIImage imageNamed:@"live_mirror"] forState:UIControlStateNormal];
    [_btnMirror addTarget:self action:@selector(clickMirror:) forControlEvents:UIControlEventTouchUpInside];
    [_btnBgView addSubview:_btnMirror];
    _btnMirror.hidden = YES;
    
    
    //log显示或隐藏
    _btnLog = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btnLog.center = CGPointMake(first_icon_center_x + icon_center_interval*5, icon_center_y);
    _btnLog.center = CGPointMake(first_icon_center_x + icon_center_interval, icon_center_y);
    _btnLog.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnLog setImage:[UIImage imageNamed:@"log"] forState:UIControlStateNormal];
    [_btnLog addTarget:self action:@selector(clickLog:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnLog];
    _btnLog.hidden = YES;
    
    
    
    // 连麦
    _btnMic = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnMic.center = CGPointMake(first_icon_center_x + icon_center_interval*2, icon_center_y);
    _btnMic.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnMic setImage:[UIImage imageNamed:@"live_open_mic"] forState:UIControlStateNormal];
    [_btnMic addTarget:self action:@selector(micClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnMic];
    _btnMic.hidden = YES;
    
    // btnAlert
    _btnAlert = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnAlert.center = CGPointMake(first_icon_center_x + icon_center_interval*5, icon_center_y);
    _btnAlert.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnAlert setImage:[UIImage imageNamed:@"live_arrow_top"] forState:UIControlStateNormal];
    [_btnAlert addTarget:self action:@selector(alertClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnAlert];
    _btnAlert.hidden = YES;
    
    //退出VC
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBtn.center = CGPointMake(first_icon_center_x + icon_center_interval*6, icon_center_y);
    _closeBtn.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_closeBtn setImage:[UIImage imageNamed:@"live_close"] forState:UIControlStateNormal];
    [_closeBtn addTarget:self action:@selector(closeVC) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBtn];

    //弹幕
    _msgTableView = [[TCMsgListTableView alloc] initWithFrame:CGRectMake(15, _btnChat.top - MSG_TABLEVIEW_HEIGHT - MSG_TABLEVIEW_BOTTOM_SPACE, MSG_TABLEVIEW_WIDTH+30, MSG_TABLEVIEW_HEIGHT) style:UITableViewStyleGrouped];
    [self addSubview:_msgTableView];
    
    _bulletViewOne = [[TCMsgBulletView alloc]initWithFrame:CGRectMake(0,_msgTableView.top - MSG_UI_SPACE - MSG_BULLETVIEW_HEIGHT, SCREEN_WIDTH, MSG_BULLETVIEW_HEIGHT)];
    [self addSubview:_bulletViewOne];
    
    _bulletViewTwo = [[TCMsgBulletView alloc]initWithFrame:CGRectMake(0, _bulletViewOne.top - MSG_BULLETVIEW_HEIGHT, SCREEN_WIDTH, MSG_BULLETVIEW_HEIGHT)];
    [self addSubview:_bulletViewTwo];
    
    
    //输入框
    _msgInputView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, MSG_TEXT_SEND_VIEW_HEIGHT )];
    _msgInputView.backgroundColor = [UIColor clearColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _msgInputView.width, _msgInputView.height)];
    imageView.image = [UIImage imageNamed:@"input_comment"];
    
    UIButton *bulletBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bulletBtn.frame = CGRectMake(10, (_msgInputView.height - MSG_TEXT_SEND_FEILD_HEIGHT)/2, MSG_TEXT_SEND_BULLET_BTN_WIDTH, MSG_TEXT_SEND_FEILD_HEIGHT);
    [bulletBtn setImage:[UIImage imageNamed:@"Switch_OFF"] forState:UIControlStateNormal];
    [bulletBtn setImage:[UIImage imageNamed:@"Switch_ON"] forState:UIControlStateSelected];
    [bulletBtn addTarget:self action:@selector(clickBullet:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sendBtn.frame = CGRectMake(self.width - 15 - MSG_TEXT_SEND_BTN_WIDTH, (_msgInputView.height - MSG_TEXT_SEND_FEILD_HEIGHT)/2, MSG_TEXT_SEND_BTN_WIDTH, MSG_TEXT_SEND_FEILD_HEIGHT);
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [sendBtn setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateNormal];
    [sendBtn setBackgroundColor:[UIColor clearColor]];
    [sendBtn addTarget:self action:@selector(clickSend) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *msgInputFeildLine1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vertical_line"]];
    msgInputFeildLine1.frame = CGRectMake(bulletBtn.right + 10, sendBtn.y, 1, MSG_TEXT_SEND_FEILD_HEIGHT);
    
    UIImageView *msgInputFeildLine2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vertical_line"]];
    msgInputFeildLine2.frame = CGRectMake(sendBtn.left - 10, sendBtn.y, 1, MSG_TEXT_SEND_FEILD_HEIGHT);
    
    _msgInputFeild = [[UITextField alloc] initWithFrame:CGRectMake(msgInputFeildLine1.right + 10,sendBtn.y,msgInputFeildLine2.left - msgInputFeildLine1.right - 20,MSG_TEXT_SEND_FEILD_HEIGHT)];
    _msgInputFeild.backgroundColor = [UIColor clearColor];
    _msgInputFeild.returnKeyType = UIReturnKeySend;
    _msgInputFeild.placeholder = @"和大家说点什么吧";
    _msgInputFeild.delegate = self;
    _msgInputFeild.textColor = [UIColor blackColor];
    _msgInputFeild.font = [UIFont systemFontOfSize:14];
    
    
    [_msgInputView addSubview:imageView];
    [_msgInputView addSubview:_msgInputFeild];
    [_msgInputView addSubview:bulletBtn];
    [_msgInputView addSubview:sendBtn];
    [_msgInputView addSubview:msgInputFeildLine1];
    [_msgInputView addSubview:msgInputFeildLine2];
    [self addSubview:_msgInputView];
    
    // 私聊
    _btnPrivateChat = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnPrivateChat.center = CGPointMake(first_icon_center_x + icon_center_interval*3, icon_center_y);
    _btnPrivateChat.bounds = CGRectMake(0, 0, icon_size, icon_size);
    [_btnPrivateChat setImage:[UIImage imageNamed:@"live_per_info"] forState:UIControlStateNormal];
    [_btnPrivateChat addTarget:self action:@selector(clickPrivateChat:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnPrivateChat];
    
    // 私聊界面
    _vChat = [[UIView  alloc] init];
    _vChat.frame = CGRectMake(0, SH(3)*2, self.width, SH(3));
    _vChat.backgroundColor = kWhiteColor;
    //    [self addSubview:_vChat];
    _vChat.hidden = YES;
    
    _chatHead = [[UILabel alloc] init];
    [_vChat addSubview:_chatHead];
    _chatHead.font = kUHSystemFontWithSize(14);
    _chatHead.textColor = kBlackColor;
    _chatHead.layer.borderColor = UISeparatedColorRGB.CGColor;
    _chatHead.text = [NSString stringWithFormat:@"私信"];
    _chatHead.textAlignment = NSTextAlignmentCenter;
    _chatHead.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    
    
//    MessageViewController *msgVC = [[MessageViewController alloc] init];
    ConversationListViewController *msgVC = [[ConversationListViewController alloc] init];
    msgVC.isLive = YES;
    _nav = [[UINavigationController alloc] initWithRootViewController:msgVC];
    _msgView = _nav.view;
    [self addSubview:_msgView];
    _msgView.hidden = YES;
    
    UIButton *closeChat = [UIButton buttonWithType:UIButtonTypeCustom];
    [_vChat addSubview:closeChat];
    [closeChat setImage:[UIImage imageNamed:@"live_zhubo_close"] forState:UIControlStateNormal];
    closeChat.frame = CGRectMake(SCREEN_WIDTH-40, 0, 40, 40);
    [closeChat addTarget:self action:@selector(maskClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineC = [[UIView alloc] initWithFrame:CGRectMake(0, _chatHead.height-1, SCREEN_WIDTH, 1)];
    [_vChat addSubview:lineC];
    lineC.backgroundColor = UISeparatedColorRGB;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, _vChat.height-40) style:UITableViewStylePlain];
    [_vChat addSubview:_tableView];
//    _tableView.delegate = self;
//    _tableView.dataSource = self;
//    _tableView.backgroundColor = kRedColor;
//    _tableView.tableFooterView = [[UIView alloc]init];
//
//    [_tableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"chatList"];
    
//    _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, _vChat.height-80) style:UITableViewStylePlain];
//    [_vChat addSubview:_tableView2];
//    _tableView2.delegate = self;
//    _tableView2.dataSource = self;
//    _tableView2.tableFooterView = [[UIView alloc]init];
//    _tableView2.hidden = YES;
//    _tableView2.backgroundColor = kYellowColor;
    
    _textBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _tableView2.height+_tableView2.y, SCREEN_WIDTH, 40)];
    [_vChat addSubview:_textBgView];
    _textBgView.backgroundColor = kWhiteColor;
//    _textBgView.hidden = YES;
    
    _privateChatTextF= [[UITextField alloc] initWithFrame:CGRectMake(5, 2, SCREEN_WIDTH-10, 36)];
    [_textBgView addSubview:_privateChatTextF];
//    _privateChatTextF.placeholder = @"我是私聊";
    _privateChatTextF.backgroundColor = UISeparatedColorRGB;
    _privateChatTextF.returnKeyType = UIReturnKeySend;
    _privateChatTextF.delegate = self;
    _privateChatTextF.layer.cornerRadius = 2;
    _privateChatTextF.layer.masksToBounds = YES;
//    _privateChatTextF.layer.borderWidth = 1;
//    _privateChatTextF.layer.borderColor = kLightGrayColor.CGColor;
    


    //美颜拉杆浮层
    float   beauty_btn_width  = 65;
    float   beauty_btn_height = 19;
#if POD_PITU
    float   beauty_btn_count  = 4;
#else
    float   beauty_btn_count  = 2;
#endif
    float   beauty_center_interval = (self.width - 30 - beauty_btn_width)/(beauty_btn_count - 1);
    float   first_beauty_center_x  = 15 + beauty_btn_width/2;
    int ib = 0;
    
    _vBeauty = [[UIView  alloc] init];
    _vBeauty.frame = CGRectMake(0, self.height-170, self.width, 170);
    [_vBeauty setBackgroundColor:[UIColor whiteColor]];
    float   beauty_center_y = _vBeauty.height - 35;
    
    _beautyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _beautyBtn.center = CGPointMake(first_beauty_center_x, beauty_center_y);
    _beautyBtn.bounds = CGRectMake(0, 0, beauty_btn_width, beauty_btn_height);
    [_beautyBtn setImage:[UIImage imageNamed:@"white_beauty"] forState:UIControlStateNormal];
    [_beautyBtn setImage:[UIImage imageNamed:@"white_beauty_press"] forState:UIControlStateSelected];
    [_beautyBtn setTitle:@"美颜" forState:UIControlStateNormal];
    [_beautyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_beautyBtn setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateSelected];
    _beautyBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    _beautyBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _beautyBtn.tag = 0;
    _beautyBtn.selected = YES;
    [_beautyBtn addTarget:self action:@selector(selectBeauty:) forControlEvents:UIControlEventTouchUpInside];
    ib++;
    
    _filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _filterBtn.center = CGPointMake(first_beauty_center_x + ib*beauty_center_interval, beauty_center_y);
    _filterBtn.bounds = CGRectMake(0, 0, beauty_btn_width, beauty_btn_height);
    [_filterBtn setImage:[UIImage imageNamed:@"beautiful"] forState:UIControlStateNormal];
    [_filterBtn setImage:[UIImage imageNamed:@"beautiful_press"] forState:UIControlStateSelected];
    [_filterBtn setTitle:@"滤镜" forState:UIControlStateNormal];
    [_filterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_filterBtn setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateSelected];
    _filterBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    _filterBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _filterBtn.tag = 1;
    _filterBtn.hidden = YES;
    [_filterBtn addTarget:self action:@selector(selectBeauty:) forControlEvents:UIControlEventTouchUpInside];
    ib++;
    
#if POD_PITU
    _motionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _motionBtn.center = CGPointMake(first_beauty_center_x + ib*beauty_center_interval, beauty_center_y);
    _motionBtn.bounds = CGRectMake(0, 0, beauty_btn_width, beauty_btn_height);
    [_motionBtn setImage:[UIImage imageNamed:@"move"] forState:UIControlStateNormal];
    [_motionBtn setImage:[UIImage imageNamed:@"move_press"] forState:UIControlStateSelected];
    [_motionBtn setTitle:@"动效" forState:UIControlStateNormal];
    [_motionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_motionBtn setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateSelected];
    _motionBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    _motionBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _motionBtn.tag = 2;
    [_motionBtn addTarget:self action:@selector(selectBeauty:) forControlEvents:UIControlEventTouchUpInside];
    ib++;
 
    _greenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _greenBtn.center = CGPointMake(first_beauty_center_x + ib*beauty_center_interval, beauty_center_y);
    _greenBtn.bounds = CGRectMake(0, 0, beauty_btn_width, beauty_btn_height);
    [_greenBtn setImage:[UIImage imageNamed:@"greens"] forState:UIControlStateNormal];
    [_greenBtn setImage:[UIImage imageNamed:@"greens_press"] forState:UIControlStateSelected];
    [_greenBtn setTitle:@"绿幕" forState:UIControlStateNormal];
    [_greenBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_greenBtn setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateSelected];
    _greenBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    _greenBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _greenBtn.tag = 3;
    [_greenBtn addTarget:self action:@selector(selectBeauty:) forControlEvents:UIControlEventTouchUpInside];
    ib++;
#endif
    
    
#if POD_PITU
    _beautyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,  _beautyBtn.top - 40, 40, 20)];
#else
    _beautyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,  _beautyBtn.top - 95, 40, 20)];
#endif
    _beautyLabel.text = @"美颜";
    _beautyLabel.font = [UIFont systemFontOfSize:12];
    _sdBeauty = [[UISlider alloc] init];
#if POD_PITU
    _sdBeauty.frame = CGRectMake(_beautyLabel.right, _beautyBtn.top - 40, self.width / 2 - _beautyLabel.right - 7, 20);
#else
    _sdBeauty.frame = CGRectMake(_beautyLabel.right, _beautyBtn.top - 95, self.width - _beautyLabel.right - 10, 20);
#endif
    _sdBeauty.minimumValue = 0;
    _sdBeauty.maximumValue = 9;
    _sdBeauty.value = 6.3;
    [_sdBeauty setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sdBeauty setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sdBeauty setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    [_sdBeauty addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    _sdBeauty.tag = 0;
    
#if POD_PITU
     _whiteLabel = [[UILabel alloc] initWithFrame:CGRectMake(_sdBeauty.right + 15, _beautyBtn.top - 40, 40, 20)];
#else
    _whiteLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _beautyBtn.top - 55, 40, 20)];
#endif
    _whiteLabel.text = @"美白";
    _whiteLabel.font = [UIFont systemFontOfSize:12];
    _sdWhitening = [[UISlider alloc] init];
#if POD_PITU
    _sdWhitening.frame =  CGRectMake(_whiteLabel.right, _beautyBtn.top - 40, self.width - _whiteLabel.right - 10, 20);
#else
    _sdWhitening.frame =  CGRectMake(_whiteLabel.right, _beautyBtn.top - 55, self.width - _whiteLabel.right - 10, 20);
#endif
    _sdWhitening.minimumValue = 0;
    _sdWhitening.maximumValue = 9;
    _sdWhitening.value = 2.7;
    [_sdWhitening setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sdWhitening setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sdWhitening setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    [_sdWhitening addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    _sdWhitening.tag = 1;
    
#if POD_PITU
    _bigEyeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _sdBeauty.top - 60, 40, 20)];
    _bigEyeLabel.text = @"大眼";
    _bigEyeLabel.font = [UIFont systemFontOfSize:12];
    _sdBigEye = [[UISlider alloc] init];
    _sdBigEye.frame =  CGRectMake(_bigEyeLabel.right, _sdBeauty.top - 60, self.width / 2 - _bigEyeLabel.right - 7, 20);
    _sdBigEye.minimumValue = 0;
    _sdBigEye.maximumValue = 9;
    _sdBigEye.value = 0;
    [_sdBigEye setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sdBigEye setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sdBigEye setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    [_sdBigEye addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    _sdBigEye.tag = 2;
    
    _slimFaceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_sdBigEye.right + 15, _sdBeauty.top - 60, 40, 20)];
    _slimFaceLabel.text = @"瘦脸";
    _slimFaceLabel.font = [UIFont systemFontOfSize:12];
    _sdSlimFace = [[UISlider alloc] init];
    _sdSlimFace.frame =  CGRectMake(_slimFaceLabel.right, _sdBeauty.top - 60, self.width - _slimFaceLabel.right - 10, 20);
    _sdSlimFace.minimumValue = 0;
    _sdSlimFace.maximumValue = 9;
    _sdSlimFace.value = 0;
    [_sdSlimFace setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sdSlimFace setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sdSlimFace setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    [_sdSlimFace addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    _sdSlimFace.tag = 3;
    

    _tmplBar = [[MCCameraDynamicView alloc] initWithFrame:CGRectMake(0.f, 0, self.width, 115.f)];
    _tmplBar.delegate = self;
    _tmplBar.hidden = YES;
    [_vBeauty addSubview:_tmplBar];
    // 美颜默认是3
    _sdBeauty.minimumValue = 0;
    _sdBeauty.maximumValue = 9;
    _sdBeauty.value = 6.3;
    
    // TODO: pitu无美白接口
    _sdWhitening.minimumValue = 0;
    _sdWhitening.maximumValue = 9;
    _sdWhitening.value = 2.7;
    
    _greenPickerView = [[V8HorizontalPickerView alloc] initWithFrame:CGRectMake(0, _beautyBtn.top - 96, self.width, 66)];
    _greenPickerView.selectedTextColor = [UIColor blackColor];
    _greenPickerView.textColor = [UIColor grayColor];
    _greenPickerView.elementFont = [UIFont fontWithName:@"" size:14];
    _greenPickerView.delegate = self;
    _greenPickerView.dataSource = self;
    _greenPickerView.hidden = YES;
    _greenPickerView.selectedMaskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greens_selected.png"]];
    _greenIndex = 0;
#endif
    
    _filterPickerView = [[V8HorizontalPickerView alloc] initWithFrame:CGRectMake(0, _beautyBtn.top - 111, self.width, 100)];

    _filterPickerView.textColor = [UIColor grayColor];
    _filterPickerView.elementFont = [UIFont fontWithName:@"" size:14];
    _filterPickerView.delegate = self;
    _filterPickerView.dataSource = self;
    _filterPickerView.hidden = YES;
    
    UIImageView *sel = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"filter_selected"]];

    _filterPickerView.selectedMaskView = sel;
    _filterType = 0;
    
    [_vBeauty addSubview:_beautyLabel];
    [_vBeauty addSubview:_whiteLabel];
    [_vBeauty addSubview:_sdWhitening];
    [_vBeauty addSubview:_sdBeauty];
    [_vBeauty addSubview:_beautyBtn];
    [_vBeauty addSubview:_bigEyeLabel];
    [_vBeauty addSubview:_sdBigEye];
    [_vBeauty addSubview:_slimFaceLabel];
    [_vBeauty addSubview:_sdSlimFace];
    [_vBeauty addSubview:_filterPickerView];
    [_vBeauty addSubview:_filterBtn];
    [_vBeauty addSubview:_motionBtn];
    [_vBeauty addSubview:_greenPickerView];
    [_vBeauty addSubview:_greenBtn];
    _vBeauty.hidden = YES;
    [self addSubview: _vBeauty];
    
    [self addSubview:_vChat];
    
    
    //********************
    // 音乐
    _vMusicPanel = [[UIView alloc] init];
    _vMusicPanel.backgroundColor = [UIColor clearColor];
    _vMusicPanel.hidden = YES;

    //***
    //BGM
    _vBGMPanel = [[UIView alloc] init];
    _vBGMPanel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];//[UIColor lightTextColor];
    
    _btnSelectBGM = [[UIButton alloc] init];
    _btnSelectBGM.titleLabel.font = [UIFont systemFontOfSize:12.f];
    _btnSelectBGM.layer.borderColor = UIColorFromRGB(0x0ACCAC).CGColor;
    [_btnSelectBGM.layer setMasksToBounds:YES];
    [_btnSelectBGM.layer setCornerRadius:6];
    [_btnSelectBGM.layer setBorderWidth:1.0];
    [_btnSelectBGM setTitle:@"伴奏" forState:UIControlStateNormal];
    [_btnSelectBGM setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateNormal];
    [_btnSelectBGM addTarget:self action:@selector(clickMusicSelect:) forControlEvents:UIControlEventTouchUpInside];
    
    _btnStopBGM = [[UIButton alloc] init];
    _btnStopBGM.titleLabel.font = [UIFont systemFontOfSize:12.f];
    _btnStopBGM.layer.borderColor = UIColorFromRGB(0x0ACCAC).CGColor;;
    [_btnStopBGM setTitle:@"结束" forState:UIControlStateNormal];
    [_btnStopBGM.layer setMasksToBounds:YES];
    [_btnStopBGM.layer setCornerRadius:6];
    [_btnStopBGM.layer setBorderWidth:1.0];
    [_btnStopBGM setTitleColor:UIColorFromRGB(0x0ACCAC) forState:UIControlStateNormal];
    [_btnStopBGM addTarget:self action:@selector(clickMusicClose:) forControlEvents:UIControlEventTouchUpInside];
    
    //***
    //音效
    _vAudioEffectPanel = [[UIView alloc] init];
    _vAudioEffectPanel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];
    
    
    _labVolumeForBGM = [[UILabel alloc] init];
    [_labVolumeForBGM setText:@"伴奏"];
    [_labVolumeForBGM setFont:[UIFont systemFontOfSize:12.f]];
    _labVolumeForBGM.textColor = UIColorFromRGB(0x0ACCAC);
    //    [_labVolumeForBGM sizeToFit];
    
    _sldVolumeForBGM = [[UISlider alloc] init];
    _sldVolumeForBGM.minimumValue = 0;
    _sldVolumeForBGM.maximumValue = 200;
    _sldVolumeForBGM.value = 100;
    [_sldVolumeForBGM setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sldVolumeForBGM setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sldVolumeForBGM setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    _sldVolumeForBGM.tag = 4;
    [_sldVolumeForBGM addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    
    _labVolumeForVoice = [[UILabel alloc] init];
    [_labVolumeForVoice setText:@"人声"];
    [_labVolumeForVoice setFont:[UIFont systemFontOfSize:12.f]];
    _labVolumeForVoice.textColor = UIColorFromRGB(0x0ACCAC);
    //    [_labVolumeForVoice sizeToFit];
    
    _sldVolumeForVoice = [[UISlider alloc] init];
    _sldVolumeForVoice.minimumValue = 0;
    _sldVolumeForVoice.maximumValue = 200;
    _sldVolumeForVoice.value = 100;
    [_sldVolumeForVoice setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
    [_sldVolumeForVoice setMinimumTrackImage:[UIImage imageNamed:@"green"] forState:UIControlStateNormal];
    [_sldVolumeForVoice setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
    _sldVolumeForVoice.tag = 5;
    [_sldVolumeForVoice addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    
    for (int i=0; i<_audioEffectArry.count; ++i) {
        UIButton *btn = [[UIButton alloc] init];
        btn.titleLabel.font = [UIFont systemFontOfSize:12.f];
        [btn setTitle:[_audioEffectArry objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"round-unselected"] forState:UIControlStateNormal];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:btn.height/2];
        [btn addTarget:self action:@selector(selectEffect:) forControlEvents:UIControlEventTouchUpInside];
        [_audioEffectViewArry addObject:btn];
        btn.tag = i;
    }
    
    _labAudioEffect = [[UILabel alloc] init];
    _labAudioEffect.textAlignment = NSTextAlignmentCenter;
    _labAudioEffect.font = [UIFont systemFontOfSize:16];
    [_labAudioEffect setText:@"音效调节"];
    _labAudioEffect.backgroundColor = [UIColor whiteColor];
    _labAudioEffect.textColor = UIColorFromRGB(0x0ACCAC);
    
    //***
    //add view
    [_vAudioEffectPanel addSubview:_labAudioEffect];
    for (int i=0; i<_audioEffectViewArry.count; ++i) {
        [_vAudioEffectPanel addSubview:[_audioEffectViewArry objectAtIndex:i]];
    }
    [_vAudioEffectPanel addSubview:_labVolumeForVoice];
    [_vAudioEffectPanel addSubview:_sldVolumeForVoice];
    [_vAudioEffectPanel addSubview:_labVolumeForBGM];
    [_vAudioEffectPanel addSubview:_sldVolumeForBGM];
    
    [_vBGMPanel addSubview:_btnSelectBGM];
    [_vBGMPanel addSubview:_btnStopBGM];
    
    [_vMusicPanel addSubview:_vAudioEffectPanel];
    [_vMusicPanel addSubview:_vBGMPanel];
    
    [self addSubview:_vMusicPanel];
    
    //***
    //音乐 layout
    [_vMusicPanel sizeWith:CGSizeMake(self.width, 275)];
    [_vMusicPanel alignParentTopWithMargin:self.height-_vMusicPanel.height];
    [_vMusicPanel alignParentLeftWithMargin:0];
    
    [_vBGMPanel sizeWith:CGSizeMake(self.width, 60)];
    [_vBGMPanel alignParentTopWithMargin:0];
    [_vBGMPanel alignParentLeftWithMargin:0];
    
    [_vAudioEffectPanel sizeWith:CGSizeMake(self.width, 215)];
    [_vAudioEffectPanel layoutBelow:_vBGMPanel];
    [_vAudioEffectPanel alignParentLeftWithMargin:0];
    
    //BMG layout
    [_btnSelectBGM sizeWith:CGSizeMake(50, 20)];
    [_btnSelectBGM alignParentTopWithMargin:10];
    [_btnSelectBGM alignParentLeftWithMargin:15];
    
    [_btnStopBGM sizeWith:CGSizeMake(50, 20)];
    [_btnStopBGM alignParentTopWithMargin:10];
    [_btnStopBGM layoutToRightOf:_btnSelectBGM margin:15];
    
    // 音效 layout
    [_labVolumeForBGM sizeWith:CGSizeMake(30, 20)];
    [_labVolumeForBGM alignParentTopWithMargin:25];
    [_labVolumeForBGM alignParentLeftWithMargin:15];
    
    [_sldVolumeForBGM sizeWith:CGSizeMake(300, 20)];
    [_sldVolumeForBGM alignParentTopWithMargin:25];
    [_sldVolumeForBGM alignParentLeftWithMargin:60];
    
    [_labVolumeForVoice sizeWith:CGSizeMake(30, 20)];
    [_labVolumeForVoice layoutBelow:_labVolumeForBGM margin:15];
    [_labVolumeForVoice alignParentLeftWithMargin:15];
    
    [_sldVolumeForVoice sizeWith:CGSizeMake(300, 20)];
    [_sldVolumeForVoice layoutBelow:_sldVolumeForBGM margin:15];
    [_sldVolumeForVoice alignParentLeftWithMargin:60];
    
    
    for (int i=0; i<_audioEffectViewArry.count; ++i) {
        UIButton *btn = (UIButton *)[_audioEffectViewArry objectAtIndex:i];
        [btn sizeWith:CGSizeMake(40,40)];
        [btn layoutBelow:_labVolumeForVoice margin:20];
        float margin = 15 + (btn.width + (self.width-(_audioEffectViewArry.count*btn.width + 30))/(_audioEffectViewArry.count-1))*i;
        [btn alignParentLeftWithMargin:margin];
    }
    
    [_labAudioEffect sizeWith:CGSizeMake(self.width, 50)];
    [_labAudioEffect layoutBelow:[_audioEffectViewArry objectAtIndex:0] margin:25];
    [_labAudioEffect alignParentLeftWithMargin:0];
    
    //********************
    
    
    //LOG UI
    _cover = [[UIView alloc]init];
    _cover.frame  = CGRectMake(10.0f, 55 + 2*icon_size, self.width - 20, self.height - 75 - 3 * icon_size);
    _cover.backgroundColor = [UIColor whiteColor];
    _cover.alpha  = 0.5;
    _cover.hidden = YES;
    [self addSubview:_cover];
    
    int logheadH = 65;
    _statusView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f, 55 + 2*icon_size, self.width - 20,  logheadH)];
    _statusView.backgroundColor = [UIColor clearColor];
    _statusView.alpha = 1;
    _statusView.textColor = [UIColor blackColor];
    _statusView.editable = NO;
    _statusView.hidden = YES;
    [self addSubview:_statusView];
    
    
    _logViewEvt = [[UITextView alloc] initWithFrame:CGRectMake(10.0f, 55 + 2*icon_size + logheadH, self.width - 20, self.height - 75 - 3 * icon_size - logheadH)];
    _logViewEvt.backgroundColor = [UIColor clearColor];
    _logViewEvt.alpha = 1;
    _logViewEvt.textColor = [UIColor blackColor];
    _logViewEvt.editable = NO;
    _logViewEvt.hidden = YES;
    [self addSubview:_logViewEvt];
    
    [self addSubview:_vBeauty]; // log挡住了美颜
}

-(void)selectBeauty:(UIButton *)button{
    switch (button.tag) {
        case 0:
        {
            _sdWhitening.hidden = NO;
            _sdBeauty.hidden    = NO;
            _beautyLabel.hidden = NO;
            _whiteLabel.hidden  = NO;
            _bigEyeLabel.hidden = NO;
            _sdBigEye.hidden    = NO;
            _slimFaceLabel.hidden = NO;
            _sdSlimFace.hidden    = NO;
            _beautyBtn.selected  = YES;
            _filterBtn.selected = NO;
            _motionBtn.selected = NO;
            _greenBtn.selected  = NO;
            _tmplBar.hidden = YES;
            _filterPickerView.hidden = YES;
            _greenPickerView.hidden = YES;
            _vBeauty.frame = CGRectMake(0, self.height-170, self.width, 170);
        }
            break;
        case 1:
        {
            _sdWhitening.hidden = YES;
            _sdBeauty.hidden    = YES;
            _beautyLabel.hidden = YES;
            _whiteLabel.hidden  = YES;
            _bigEyeLabel.hidden = YES;
            _sdBigEye.hidden    = YES;
            _slimFaceLabel.hidden = YES;
            _sdSlimFace.hidden    = YES;
            _beautyBtn.selected  = NO;
            _filterBtn.selected = YES;
            _motionBtn.selected = NO;
            _greenBtn.selected  = NO;
            _tmplBar.hidden = YES;
            _filterPickerView.hidden = NO;
            _greenPickerView.hidden = YES;
            [_filterPickerView scrollToElement:_filterType animated:NO];
        }
            break;
        case 2: {
            _sdWhitening.hidden = YES;
            _sdBeauty.hidden    = YES;
            _beautyLabel.hidden = YES;
            _whiteLabel.hidden  = YES;
            _bigEyeLabel.hidden = YES;
            _sdBigEye.hidden    = YES;
            _slimFaceLabel.hidden = YES;
            _sdSlimFace.hidden    = YES;
            _beautyBtn.selected  = NO;
            _filterBtn.selected = NO;
            _motionBtn.selected = YES;
            _greenBtn.selected  = NO;
            _tmplBar.hidden = NO;
            _filterPickerView.hidden = YES;
            _greenPickerView.hidden = YES;
        }
            break;
        case 3: {
            _sdWhitening.hidden = YES;
            _sdBeauty.hidden    = YES;
            _beautyLabel.hidden = YES;
            _whiteLabel.hidden  = YES;
            _bigEyeLabel.hidden = YES;
            _sdBigEye.hidden    = YES;
            _slimFaceLabel.hidden = YES;
            _sdSlimFace.hidden    = YES;
            _beautyBtn.selected  = NO;
            _filterBtn.selected = NO;
            _motionBtn.selected = NO;
            _greenBtn.selected  = YES;
            _tmplBar.hidden = YES;
            _filterPickerView.hidden = YES;
            _greenPickerView.hidden = NO;
            [_greenPickerView scrollToElement:_greenIndex animated:NO];
        }
        default:
            break;
    }
    _beautyBtn.center = CGPointMake(_beautyBtn.center.x, _vBeauty.frame.size.height - 35);
    _filterBtn.center = CGPointMake(_filterBtn.center.x, _vBeauty.frame.size.height - 35);
    _greenBtn.center = CGPointMake(_greenBtn.center.x, _vBeauty.frame.size.height - 35);
    _motionBtn.center = CGPointMake(_motionBtn.center.x, _vBeauty.frame.size.height - 35);
}

- (void)selectEffect:(UIButton *)button {
    for (int i=0; i<_audioEffectViewArry.count; ++i) {
        UIButton *btn = (UIButton *)[_audioEffectViewArry objectAtIndex:i];
        btn.selected = NO;
        [btn setBackgroundImage:[UIImage imageNamed:@"round-unselected"] forState:UIControlStateNormal];
    }
    button.selected = YES;
    [button setBackgroundImage:[UIImage imageNamed:@"round-selected"] forState:UIControlStateNormal];
    
    _audioEffectSelectedType = button.tag;
    
    if (self.delegate) [self.delegate selectEffect:_audioEffectSelectedType];
}

-(void)bulletMsg:(TCMsgModel *)msgModel{
    [_msgTableView bulletNewMsg:msgModel];
    
    if (msgModel.msgType == TCMsgModelType_DanmaMsg) {
        if ([self getLocation:_bulletViewOne] >= [self getLocation:_bulletViewTwo]) {
            [_bulletViewTwo bulletNewMsg:msgModel];
        }else{
            [_bulletViewOne bulletNewMsg:msgModel];
        }
    }
    
    if (msgModel.msgType == TCMsgModelType_MemberEnterRoom || msgModel.msgType == TCMsgModelType_MemberQuitRoom) {
        [_audienceTableView refreshAudienceList:msgModel];
    }
}

-(CGFloat)getLocation:(TCMsgBulletView *)bulletView{
    UIView *view = bulletView.lastAnimateView;
    CGRect rect = [view.layer.presentationLayer frame];
    return rect.origin.x + rect.size.width;
}

-(void)clickBullet:(UIButton *)btn{
    _bulletBtnIsOn = !_bulletBtnIsOn;
    btn.selected = _bulletBtnIsOn;
}

-(void)clickChat:(UIButton *)button{
    [_msgInputFeild becomeFirstResponder];
}

-(void)clickSend{
    [self textFieldShouldReturn:_msgInputFeild];
}


-(void)showLikeHeart{
    int x = (_btnLog.frame.origin.x + _closeBtn.frame.origin.x) / 2;
    CGRect rect = CGRectMake(x, _closeBtn.frame.origin.y, _closeBtn.frame.size.width, _closeBtn.frame.size.height);
    [self showLikeHeartStartRect:rect];
}

- (void)showLikeHeartStartRect:(CGRect)frame
{
    {
        // 星星动画频率限制
        static TCFrequeControl *freqControl = nil;
        if (freqControl == nil) {
            freqControl = [[TCFrequeControl alloc] initWithCounts:10 andSeconds:1];
        }
        
        if (![freqControl canTrigger]) {
            return;
        }
    }

    
    if (_viewsHidden) {
        return;
    }
    UIImageView *imageView = [[UIImageView alloc ] initWithFrame:frame];
    imageView.image = [[UIImage imageNamed:@"img_like"] imageWithTintColor:[UIColor randomFlatDarkColor]];
    [self.superview addSubview:imageView];
    imageView.alpha = 0;
    
    
    [imageView.layer addAnimation:[self hearAnimationFrom:frame] forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [imageView removeFromSuperview];
    });
}

- (CAAnimation *)hearAnimationFrom:(CGRect)frame
{
    //位置
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.beginTime = 0.5;
    animation.duration = 2.5;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount= 0;
    animation.calculationMode = kCAAnimationCubicPaced;
    
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPoint point0 = CGPointMake(frame.origin.x + frame.size.width / 2, frame.origin.y + frame.size.height / 2);
    
    CGPathMoveToPoint(curvedPath, NULL, point0.x, point0.y);
    
    if (!_heartAnimationPoints) {
        _heartAnimationPoints = [[NSMutableArray alloc] init];
    }
    if ([_heartAnimationPoints count] < 40) {
        float x11 = point0.x - arc4random() % 30 + 30;
        float y11 = frame.origin.y - arc4random() % 60 ;
        float x1 = point0.x - arc4random() % 15 + 15;
        float y1 = frame.origin.y - arc4random() % 60 - 30;
        CGPoint point1 = CGPointMake(x11, y11);
        CGPoint point2 = CGPointMake(x1, y1);
        
        int conffset2 = self.superview.bounds.size.width * 0.2;
        int conffset21 = self.superview.bounds.size.width * 0.1;
        float x2 = point0.x - arc4random() % conffset2 + conffset2;
        float y2 = arc4random() % 30 + 240;
        float x21 = point0.x - arc4random() % conffset21  + conffset21;
        float y21 = (y2 + y1) / 2 + arc4random() % 30 - 30;
        CGPoint point3 = CGPointMake(x21, y21);
        CGPoint point4 = CGPointMake(x2, y2);
        
        [_heartAnimationPoints addObject:[NSValue valueWithCGPoint:point1]];
        [_heartAnimationPoints addObject:[NSValue valueWithCGPoint:point2]];
        [_heartAnimationPoints addObject:[NSValue valueWithCGPoint:point3]];
        [_heartAnimationPoints addObject:[NSValue valueWithCGPoint:point4]];
    }
    
    // 从_heartAnimationPoints中随机选取一组point
    int idx = arc4random() % ([_heartAnimationPoints count]/4);
    CGPoint p1 = [[_heartAnimationPoints objectAtIndex:4*idx] CGPointValue];
    CGPoint p2 = [[_heartAnimationPoints objectAtIndex:4*idx+1] CGPointValue];
    CGPoint p3 = [[_heartAnimationPoints objectAtIndex:4*idx+2] CGPointValue];
    CGPoint p4 = [[_heartAnimationPoints objectAtIndex:4*idx+3] CGPointValue];
    CGPathAddQuadCurveToPoint(curvedPath, NULL, p1.x, p1.y, p2.x, p2.y);
    CGPathAddQuadCurveToPoint(curvedPath, NULL, p3.x, p3.y, p4.x, p4.y);

    
    animation.path = curvedPath;
    
    CGPathRelease(curvedPath);
    
    //透明度变化
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnim.toValue = [NSNumber numberWithFloat:0];
    opacityAnim.removedOnCompletion = NO;
    opacityAnim.beginTime = 0;
    opacityAnim.duration = 3;
    
    //比例
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //        int scale = arc4random() % 5 + 5;
    scaleAnim.fromValue = [NSNumber numberWithFloat:.0];//[NSNumber numberWithFloat:((float)scale / 10)];
    scaleAnim.toValue = [NSNumber numberWithFloat:1];
    scaleAnim.removedOnCompletion = NO;
    scaleAnim.fillMode = kCAFillModeForwards;
    scaleAnim.duration = .5;
    
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects: scaleAnim,opacityAnim,animation, nil];
    animGroup.duration = 3;
    
    return animGroup;
}

-(BOOL)isAlreadyInAudienceList:(TCMsgModel *)model
{
    return [_audienceTableView isAlreadyInAudienceList:model];
}

//监听键盘高度变化
-(void)keyboardFrameDidChange:(NSNotification*)notice
{
    NSDictionary * userInfo = notice.userInfo;
    NSValue * endFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect endFrame = endFrameValue.CGRectValue;
    [UIView animateWithDuration:0.25 animations:^{
        if (endFrame.origin.y == self.height) {
            _msgInputView.y =  endFrame.origin.y;
        }else{
            if (_vChat.isHidden == YES) {
                _msgInputView.y =  endFrame.origin.y - _msgInputView.height;
            }
        }
    }];
}

// 监听登出消息
- (void)onLogout:(NSNotification*)notice {
    [self closeInternal];
}

#pragma mark TCPushDecorateDelegate
-(void)closeVC{
    _closeAlert = [[UIAlertView alloc] initWithTitle:nil message:kTipsMsgStopPush  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [_closeAlert show];
}

- (void)closeVCWithError:(NSString *)msg Alert:(BOOL)isAlert Result:(BOOL)isShowResult{
    if (isAlert) {
        if (isShowResult) {
            _closeErrRstAlert = [[UIAlertView alloc] initWithTitle:nil message:msg  delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [_closeErrRstAlert show];
        } else {
            _closeErrAlert = [[UIAlertView alloc] initWithTitle:nil message:msg  delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [_closeErrAlert show];
        }
    } else {
//        [[HUDHelper sharedInstance] tipMessage:msg];
        [self closeInternal];
    }
}

- (void)enableMix:(BOOL)disable
{
    [_btnMusic setEnabled:disable];
    
    _vMusicPanel.hidden = YES;
}

- (void)closeInternal {
    [_msgHandler releaseIMRef];
    [_topView pauseLive];
    [_bulletViewOne stopAnimation];
    [_bulletViewTwo stopAnimation];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //if(self.delegate) [self.delegate closeRTMP];
    if(self.delegate) [self.delegate closeVC];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ( (buttonIndex == 1 && alertView == _closeAlert)
        || (buttonIndex == 0 && alertView == _closeErrAlert)
        || (buttonIndex == 0 && alertView == _closeErrRstAlert)) {
        if (self.delegate) {
            [_msgHandler releaseIMRef];
            [_topView pauseLive];
            [_bulletViewOne stopAnimation];
            [_bulletViewTwo stopAnimation];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
           
            //[self.delegate closeRTMP];
            
//            if (alertView == _closeAlert || alertView == _closeErrRstAlert) {
//                // 直播过程中退出时展示统计信息
//                __weak __typeof(self) weakSelf = self;
//                _resultView = [[TCPushShowResultView alloc] initWithFrame:self.bounds resultData:_topView backHomepage:^{
//                    [weakSelf.delegate closeVC];
//                }];
//            } else if (alertView == _closeErrAlert) {
                [self.delegate closeVC];
//            }
            
//            [self addSubview:_resultView];
        }
    }
}

-(void)clickScreen:(UITapGestureRecognizer *)gestureRecognizer{
    [_msgInputFeild resignFirstResponder];
    if (self.delegate) {
        [self.delegate clickScreen:gestureRecognizer];
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

    if (_vBeauty.hidden == NO && CGRectContainsPoint(_vBeauty.frame, point)) {
        [self removeGestureRecognizer:_tap];
    }
    
    return YES;
}


-(void)clickCamera:(UIButton *)button{
    if (self.delegate)  {
        [self.delegate clickCamera:button];
        // 切换摄像头自动更改镜像
//        [self.delegate clickMirror:_btnMirror];
    }
}

-(void)clickBeauty:(UIButton *)button{
    _btnBgView.hidden = YES;
    _btnAlert.selected = 0;
    if (self.delegate) [self.delegate clickBeauty:button];
}

- (void)clickMusic:(UIButton *)button {
    
    _btnBgView.hidden = YES;
    _btnAlert.selected = 0;
    [self selectEffect:[_audioEffectViewArry objectAtIndex:_audioEffectSelectedType]];
    if (self.delegate) [self.delegate clickMusic:button];
}

- (void)clickMusicSelect:(UIButton *)button {
    if (self.delegate) [self.delegate clickMusicSelect:button];
}

- (void)clickMusicClose:(UIButton *)button {
    if (self.delegate) [self.delegate clickMusicClose:button];
}

-(void)clickTorch:(UIButton *)button{
    if (self.delegate) [self.delegate clickTorch:button];
}

-(void)clickLog:(UIButton *)button{
    if (self.delegate) [self.delegate clickLog:button];
}
-(void)sliderValueChange:(UISlider*) slider{
    if (self.delegate) [self.delegate sliderValueChange:slider];
}
-(void)sliderValueChangeEx:(UISlider*) slider{
    if (self.delegate) [self.delegate sliderValueChangeEx:slider];
}

- (void)motionTmplSelected:(NSString *)materialID {
    if ([self.delegate respondsToSelector:@selector(motionTmplSelected:)]) {
        [self.delegate motionTmplSelected:materialID];
    }
}
-(void)contributionClick:(UIButton *)sender {
    NSLog(@"点击贡献榜");
}
- (void)alertClick:(UIButton *)sender {
    NSLog(@"点击弹出 %d", sender.isSelected);
    _btnBgView.hidden = sender.isSelected;
    sender.selected = !sender.isSelected;
}

- (void)clickPrivateChat:(UIButton *)button {
    NSLog(@"点击私信");
    
    [_btnPrivateChat setImage:[UIImage imageNamed:@"live_per_info"] forState:UIControlStateNormal];
    
//    _vChat.hidden = NO;
//    _btnAlert.selected = 0;
//    _tableView2.hidden = YES;
//    _textBgView.hidden = YES;
//    [_btnPrivateChat setImage:[UIImage imageNamed:@"live_per_info"] forState:UIControlStateNormal];
//    
//    [self chatListConversations];
    
//    [self addSubview:_msgView];
    _msgView.hidden = NO;
    [self bringSubviewToFront:_msgView];
    
}

- (void)chatListConversations {
    
    NSArray * conversations = [[TIMManager sharedInstance] getConversationList];
    [self.conversationArr removeAllObjects];
    [self.chatListArr removeAllObjects];
    for (TIMConversation *conversation in conversations) {
        
        // 如果是c2c就输出
        if (conversation.getType == 1) {
            NSLog(@"%@", conversation);
            
            [self.conversationArr addObject:conversation];
            NSMutableArray *listArr = [[NSMutableArray alloc] init];
            
            [conversation getMessage:1 last:nil succ:^(NSArray *msgs) {
                
                for (TIMMessage *msg in msgs) {
                    //                    NSLog(@"====%@", msg);
                    
                    for(int index = 0; index < [msg elemCount]; index++) {
                        TIMElem *elem = [msg getElem:index];
                        
                        if([elem isKindOfClass:[TIMTextElem class]]) {
                            // 消息总入口频率限制
                            static TCFrequeControl *freqControl = nil;
                            if (freqControl == nil) {
                                freqControl = [[TCFrequeControl alloc] initWithCounts:20 andSeconds:1];
                            }
                            if (![freqControl canTrigger]) {
                                return;
                            }
                            // 文本消息
                            TIMTextElem *textElem = (TIMTextElem *)elem;
                            NSString *msgText = textElem.text;
                            NSDictionary* dict = [TCUtil jsonData2Dictionary:msgText];
                            
                            //                            NSLog(@"获取聊天信息 = %@", dict);
                            
                            if (dict == nil) {
                                return;
                            }
                            
                            NSError *err;
                            NSData *jsonData = [[dict valueForKey:@"senderInfo"] dataUsingEncoding:NSUTF8StringEncoding];
                            NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                                     options:NSJSONReadingMutableContainers
                                                                                       error:&err];
                            
                            NSString *currId = [GetUserDefaule(USERINFO) valueForKey:@"user_id"];
                            
                            NSLog(@"当前用户id %@", currId);
                            
                            if ([currId isEqualToString:infoDict[@"uid"]]) {
                                NSData *jsonData = [[dict valueForKey:@"receiverInfo"] dataUsingEncoding:NSUTF8StringEncoding];
                                NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                                         options:NSJSONReadingMutableContainers
                                                                                           error:&err];
                                [listArr addObject:dict[@"msg"]];
                                [listArr addObject:infoDict];
                            }
                            else {
                                [listArr addObject:dict[@"msg"]];
                                [listArr addObject:infoDict];
                            }
                            
                            [self.chatListArr addObject:listArr];
                        }
                        else if([elem isKindOfClass:[TIMCustomElem class]]) {
                            // 自定义消息
                            TIMCustomElem* cele = (TIMCustomElem*)elem;
                            NSString *dataStr = [[NSString alloc] initWithData:cele.data encoding:NSUTF8StringEncoding];
                            DebugLog(@"datastr is:%@", dataStr);
                        }
                    }
                }
                
                [_tableView reloadData];
                
            } fail:^(int code, NSString *msg) {
                NSLog(@"%d, %@", code, msg);
            }];
        }
    }
    
}

- (void)micClick:(UIButton *)button {
    NSLog(@"点击麦克风");
}

-(void)clickMirror:(UIButton *)btn{
    NSLog(@"点击镜像");
    if (self.delegate) [self.delegate clickMirror:btn];
}

#pragma mark - HorizontalPickerView DataSource Methods/Users/annidy/Work/RTMPDemo_PituMerge/RTMPSDK/webrtc
- (NSInteger)numberOfElementsInHorizontalPickerView:(V8HorizontalPickerView *)picker {
    if (picker == _greenPickerView) {
        return [_greenArray count];
    } else if(picker == _filterPickerView) {
        return [_filterArray count];
    }
    return 0;
}

#pragma mark - HorizontalPickerView Delegate Methods
//- (NSString *)horizontalPickerView:(V8HorizontalPickerView *)picker titleForElementAtIndex:(NSInteger)index {
//    if (picker == _greenPickerView) {
//        return [[_greenArray objectAtIndex:index] title];
//    } else if(picker == _filterPickerView) {
////        return [[_filterArray objectAtIndex:index] title];
//        return [_filterArray objectAtIndex:index];
//    }
//    return nil;
//}

- (UIView *)horizontalPickerView:(V8HorizontalPickerView *)picker viewForElementAtIndex:(NSInteger)index {
    if (picker == _greenPickerView) {
        V8LabelNode *v = [_greenArray objectAtIndex:index];
        return [[UIImageView alloc] initWithImage:v.face];
    } else if(picker == _filterPickerView) {
        V8LabelNode *v = [_filterArray objectAtIndex:index];
        return [[UIImageView alloc] initWithImage:v.face];
    }
    return nil;
}

- (NSInteger) horizontalPickerView:(V8HorizontalPickerView *)picker widthForElementAtIndex:(NSInteger)index {
    if (picker == _greenPickerView) {
        return 70;
    }
    return 90;
}

- (void)horizontalPickerView:(V8HorizontalPickerView *)picker didSelectElementAtIndex:(NSInteger)index {
    if (picker == _greenPickerView) {
        _greenIndex = index;
        if ([self.delegate respondsToSelector:@selector(greenSelected:)]) {
            V8LabelNode *v = [_greenArray objectAtIndex:index];
            [self.delegate greenSelected:v.file];
        }
    } else if(picker == _filterPickerView) {
        _filterType = index;
        if ([self.delegate respondsToSelector:@selector(filterSelected:)]) {
            [self.delegate filterSelected:index];
        }
    }
}

#pragma mark - AVIMMsgListener

-(void)onRecvGroupSender:(IMUserAble *)info textMsg:(NSString *)msgText{
    
    
    
    switch (info.cmdType) {
        case AVIMCMD_Custom_Text: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = [info imUserName];
            msgModel.userMsg  =  msgText;
            msgModel.userHeadImageUrl = info.imUserIconUrl;
            msgModel.level = [NSString stringWithFormat:@"%@", info.level];
            msgModel.msgType = TCMsgModelType_NormalMsg;
            [self bulletMsg:msgModel];
            break;
        }
            
        case AVIMCMD_Custom_EnterLive: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = info.imUserName;
            msgModel.userMsg  =  @"加入直播";
            msgModel.userHeadImageUrl = info.imUserIconUrl;
//            msgModel.level = info.level;
            msgModel.msgType = TCMsgModelType_MemberEnterRoom;
            
            //收到新增观众消息，判断只有没在观众列表中，数量才需要增加1
            if (![self isAlreadyInAudienceList:msgModel])
            {
                [_topView onUserEnterLiveRoom];
            }
            [self bulletMsg:msgModel];
            
            break;
        }

        case AVIMCMD_Custom_ExitLive: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = info.imUserName;
            msgModel.userMsg  =  @"退出直播";
            msgModel.userHeadImageUrl = info.imUserIconUrl;
//            msgModel.level = info.level;
            msgModel.msgType = TCMsgModelType_MemberQuitRoom;
            
            [self bulletMsg:msgModel];
            [_topView onUserExitLiveRoom];
            
            break;
        }
            
        case AVIMCMD_Custom_Like: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = [info imUserName];
            msgModel.userMsg  =  @"点了个赞";
            msgModel.userHeadImageUrl = info.imUserIconUrl;
//            msgModel.level = info.level;
            msgModel.msgType = TCMsgModelType_Praise;
            
            [self bulletMsg:msgModel];
            [self showLikeHeart];
            [_topView onUserSendLikeMessage];
            break;
        }
            
        case AVIMCMD_Custom_Favorites: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = [info imUserName];
            msgModel.userMsg  =  @"关注了你";
            msgModel.userHeadImageUrl = info.imUserIconUrl;
            //            msgModel.level = info.level;
            msgModel.msgType = TCMsgModelType_Favorites;
            
            [self bulletMsg:msgModel];
            break;
        }
            
        case AVIMCMD_Custom_Danmaku: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = [info imUserName];
            msgModel.userMsg  =  msgText;
            msgModel.userHeadImageUrl = info.imUserIconUrl;
            msgModel.level = info.level;
            msgModel.msgType = TCMsgModelType_DanmaMsg;
            
            [self bulletMsg:msgModel];

            break;
        }
            
        case AVIMCMD_Custom_Gift: {
            TCMsgModel *msgModel = [[TCMsgModel alloc] init];
            msgModel.userId = info.uid;
            msgModel.userName = [info imUserName];
            msgModel.userMsg  =  msgText;
            msgModel.userHeadImageUrl = info.imUserIconUrl;
            msgModel.msgType = TCMsgModelType_Gift;
            
            _xiubi.text = [NSString stringWithFormat:@"秀币 %@个", [msgText valueForKey:@"stars_count"]];
//            CGSize coinSize = [LZCTools textSizeWithText:_xiubi.text font:kUHSystemFontWithSize(10)];
//            _xiubi.width = coinSize.width;
            
            NSDictionary *giftDict = @{@"msg":msgModel.userMsg, @"nickName":msgModel.userName, @"headPic":msgModel.userHeadImageUrl, @"userId":[GetUserDefaule(USERINFO) valueForKey:@"user_id"]};
            // 礼物发送消息通知
            NSNotification *notification =[NSNotification notificationWithName:@"showGift" object:nil userInfo:giftDict];
            //通过通知中心发送通知
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
            break;
        }
            
        default:
            break;
    }
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    _msgInputFeild.text = @"";
    _privateChatTextF.text = @"";
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    _msgInputFeild.text = textField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isEqual:_msgInputFeild]) {

        NSString *textMsg = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//        NSString *textMsg = @"为什么!!!!!!!!!!!!!1";
        if (textMsg.length <= 0) {
            textField.text = @"";
            [HUDHelper alert:@"消息不能为空"];
            return YES;
        }

        NSDictionary *userInfo = GetUserDefaule(USERINFO);
        TCMsgModel *msgModel = [[TCMsgModel alloc] init];
        msgModel.userName = @"我";
        msgModel.userMsg  =  textMsg;
        msgModel.userHeadImageUrl = userInfo[@"face"];
        msgModel.level = [NSString stringWithFormat:@"%@", userInfo[@"level"]];
        
        if (_bulletBtnIsOn) {
            msgModel.msgType  = TCMsgModelType_DanmaMsg;
            [_msgHandler sendDanmakuSenderInfo:_liveInfo.senderInfo msg:textMsg];
        }else{
            msgModel.msgType = TCMsgModelType_NormalMsg;
            [_msgHandler sendTextSenderInfo:_liveInfo.senderInfo msg:textMsg];
        }
        NSLog(@"++++++%@, %@, %@, %@", textMsg, textField.text, msgModel, userInfo);
        
        [self bulletMsg:msgModel];
        
        [_msgInputFeild resignFirstResponder];
        
    }
    else {
        [_msgHandler sendC2CSenderInfo:_liveInfo.senderInfo toSenderInfo:_receiveInfo msg:textField.text];
        [_privateChatTextF resignFirstResponder];
        _privateChatTextF.text = @"";
        [self getConversation:_currConversation];
    }

    
    return YES;
}

#pragma mark - 滑动隐藏界面UI

- (void)maskClick {
    _vChat.hidden = YES;
}


/*
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    _touchBeginLocation = [touch locationInView:self];
    _touchPrevLocation = _touchBeginLocation;
    
    if (CGRectContainsPoint(_vMusicPanel.frame, _touchBeginLocation)) {
        _isTouchMusicPanel = YES;
    }
    if (_vBeauty.hidden == NO) {
        _vBeauty.hidden = YES;
        //[self addGestureRecognizer:_tap];
    }
    if (_vChat.hidden == NO) {
        _vChat.hidden = YES;
        //[self addGestureRecognizer:_tap];
    }
    
    _chatHead.text= @"私信";
    [_privateChatTextF resignFirstResponder];
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    
    if (!_isTouchMusicPanel) {
        [self endMove:location.x - _touchBeginLocation.x];
    }
    else {
        _isTouchMusicPanel = NO;
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    CGPoint prevLocation = [touch previousLocationInView:self];
    
    // 拖动音乐浮窗
    if (_isTouchMusicPanel) {
        CGRect frame = _vMusicPanel.frame;
        CGPoint newOriginPoint = CGPointMake(frame.origin.x + location.x - prevLocation.x, frame.origin.y + location.y - prevLocation.y);

        if (10 <= newOriginPoint.x && newOriginPoint.x + frame.size.width <= SCREEN_WIDTH - 10
            && 50 <= newOriginPoint.y && newOriginPoint.y + frame.size.height <= SCREEN_HEIGHT - 30) {
            frame.origin = newOriginPoint;
            [_vMusicPanel setFrame:frame];
        }
    }
}

-(void)endMove:(CGFloat)moveX{
    [UIView animateWithDuration:0.2 animations:^{
        if(moveX > 10){
            for (UIView *view in self.subviews) {
                if (![view isEqual:_closeBtn] && ![view isEqual:_resultView]) {
                    CGRect rect = view.frame;
                    if (rect.origin.x >= 0 && rect.origin.x < SCREEN_WIDTH) {
                        rect = CGRectOffset(rect, self.width, 0);
                        view.frame = rect;
                        [self resetViewAlpha:view];
                    }
                }
            }
        }else if(moveX < -10){
            for (UIView *view in self.subviews) {
                if (![view isEqual:_closeBtn] && ![view isEqual:_resultView]) {
                    CGRect rect = view.frame;
                    if (rect.origin.x >= SCREEN_WIDTH) {
                        rect = CGRectOffset(rect, -self.width, 0);
                        view.frame = rect;
                        [self resetViewAlpha:view];
                    }
                    
                }
            }
        }
    }];
}

-(void)resetViewAlpha:(UIView *)view{
    CGRect rect = view.frame;
    if (rect.origin.x  >= SCREEN_WIDTH || rect.origin.x < 0) {
        view.alpha = 0;
        _viewsHidden = YES;
    }else{
        view.alpha = 1;
        _viewsHidden = NO;
    }
    if (view == _cover)
        _cover.alpha = 0.5;
}

*/


#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([_tableView isEqual:tableView]) {
        
        MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chatList"];
        NSArray *arr = self.chatListArr[indexPath.row];
        NSDictionary *dict = arr[1];
        cell.nickName.text = dict[@"nickname"];
        [cell.headPic sd_setImageWithURL:[NSURL URLWithString:dict[@"headpic"]] placeholderImage:IMAGE(@"placeHolder")];
        cell.sign.text = arr[0];
        cell.date.hidden = YES;
        
        
        cell.contentView.tag = indexPath.row;
        [cell.contentView addTarget:self action:@selector(tableViewCellClick:)];
        
        return cell;
    }
    else {
        
        UITableViewCell *cell;
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        else{
            for (UIView *cellView in cell.subviews){
                [cellView removeFromSuperview];
            }
        }

        
        NSArray *arr = self.chatContentArr[indexPath.row];
        NSDictionary *dict = arr[1];
        
        //创建头像
        UIImageView *photo ;
        // 当前用户的
        if ([dict[@"uid"] isEqualToString:[GetUserDefaule(USERINFO) valueForKey:@"user_id"]]) {
            photo = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 50)];
            photo.layer.cornerRadius = 25;
            photo.layer.masksToBounds = YES;
            [cell addSubview:photo];
            [photo sd_setImageWithURL:[NSURL URLWithString:dict[@"headpic"]] placeholderImage:IMAGE(@"placeHolder")];
        
            // 解析内容            // 文本
            [cell addSubview:[self bubbleView:arr[0] from:YES withPosition:65]];
            
            
        }else{
            photo = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
            photo.layer.cornerRadius = 25;
            photo.layer.masksToBounds = YES;
            [cell addSubview:photo];
            [photo sd_setImageWithURL:[NSURL URLWithString:[[dict valueForKey:@"user_info"] valueForKey:@"face"]] placeholderImage:[UIImage imageNamed:@"placeHlder.png"]];
            
            [photo sd_setImageWithURL:[NSURL URLWithString:dict[@"headpic"]] placeholderImage:IMAGE(@"placeHolder")];
            
            // 解析内容
            [cell addSubview:[self bubbleView:arr[0] from:NO withPosition:65]];
            
        }

        
        
        return cell;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:_tableView]) {
        return self.chatListArr.count;
    }
    else
        return self.chatContentArr.count;
//    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_tableView]) {
        return SW(9)+20;
    }
    
    else if ([tableView isEqual:_tableView2]) {
        
        NSArray *arr = self.chatContentArr[indexPath.row];
//        NSDictionary *dict = arr[1];
//        NSDictionary *dict = [_dataArr objectAtIndex:indexPath.row];
        UIFont *font = [UIFont systemFontOfSize:14];
        CGSize size;
        size = [arr[0] sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
        
        return size.height+44;

        
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
    
    if ([tableView isEqual:_tableView]) {
        _tableView2.hidden = NO;
    }
    
}

- (void)tableViewCellClick:(UITableViewCell *)sender {
    
    _tableView2.hidden = NO;
    _textBgView.hidden = NO;
//    _chatHead.text = sender.textLabel.text;
    
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
    
    NSInteger tag = [tapRecognizer.view tag];
    
    NSArray *arr = self.chatListArr[tag];
    NSDictionary *dict = arr[1];
    _chatHead.text = dict[@"nickname"];
//    _chatHead.textAlignment = NSTextAlignmentCenter;
    
    TIMConversation *conversation = self.conversationArr[tag];
    _currConversation = conversation;
    
    [self getConversation:conversation];
    
}


//泡泡文本
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf withPosition:(int)position{
    
    //计算大小
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
    
    // build single chat bubble cell with given text
    UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
    returnView.backgroundColor = [UIColor clearColor];
    
    //背影图片
    UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
    
    UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:floorf(bubble.size.width/2) topCapHeight:floorf(bubble.size.height/2)]];
    //    NSLog(@"%f,%f",size.width,size.height);
    
    
    //添加文本信息
    UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(fromSelf?15.0f:22.0f, 20.0f, size.width+10, size.height+10)];
    bubbleText.backgroundColor = [UIColor clearColor];
    bubbleText.font = font;
    bubbleText.numberOfLines = 0;
    bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
    //    bubbleText.text = text;
//    bubbleText.text = [EaseConvertToCommonEmoticonsHelper convertToSystemEmoticons:text];
    bubbleText.text = text;
    
    bubbleImageView.frame = CGRectMake(0.0f, 14.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+20.0f);
    
    if(fromSelf)
        returnView.frame = CGRectMake(SCREEN_WIDTH-position-(bubbleText.frame.size.width+30.0f), 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
    else
        returnView.frame = CGRectMake(position, 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
    
    [returnView addSubview:bubbleImageView];
    [returnView addSubview:bubbleText];
    
    return returnView;
}



- (void)getConversation:(TIMConversation *)conversation {
    
    [self.chatContentArr removeAllObjects];
    
    [conversation getMessage:10 last:nil succ:^(NSArray *msgs) {
        for (TIMMessage *msg in msgs) {
            NSMutableArray *listArr = [[NSMutableArray alloc] init];
            for(int index = 0; index < [msg elemCount]; index++) {
                TIMElem *elem = [msg getElem:index];
                if([elem isKindOfClass:[TIMTextElem class]]) {
                    // 消息总入口频率限制
                    static TCFrequeControl *freqControl = nil;
                    if (freqControl == nil) {
                        freqControl = [[TCFrequeControl alloc] initWithCounts:20 andSeconds:1];
                    }
                    if (![freqControl canTrigger]) {
                        return;
                    }
                    // 文本消息
                    TIMTextElem *textElem = (TIMTextElem *)elem;
                    NSString *msgText = textElem.text;
                    NSDictionary* dict = [TCUtil jsonData2Dictionary:msgText];
                    NSLog(@"获取聊天信息 = %@", dict);
                    if (dict == nil) {
                        return;
                    }
                    NSError *err;
                    NSData *jsonData = [[dict valueForKey:@"senderInfo"] dataUsingEncoding:NSUTF8StringEncoding];
                    NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                             options:NSJSONReadingMutableContainers
                                                                               error:&err];
                    //                    NSLog(@"---Json = %@", [dict valueForKey:@"senderInfo"]);
                    //                    [self.chatContentArr addObject:infoDict];
//                    _receiveInfo = dict[@"receiverInfo"];
                    
                    
                    NSString *currId = [GetUserDefaule(USERINFO) valueForKey:@"user_id"];
                    
                    NSLog(@"当前用户id %@", currId);
                    
                    if ([currId isEqualToString:infoDict[@"uid"]]) {
                        _receiveInfo = dict[@"receiverInfo"];
                    }
                    else {
                        _receiveInfo = dict[@"senderInfo"];
                    }
                    
                    [listArr addObject:dict[@"msg"]];
                    [listArr addObject:infoDict];
                    
//                    [self.chatContentArr addObject:dict[@"msg"]];
                    [self.chatContentArr addObject:listArr];
                }
                else if([elem isKindOfClass:[TIMCustomElem class]]) {
                    // 自定义消息
                    TIMCustomElem* cele = (TIMCustomElem*)elem;
                    NSString *dataStr = [[NSString alloc] initWithData:cele.data encoding:NSUTF8StringEncoding];
                    DebugLog(@"datastr is:%@", dataStr);
                }
            }
        }
        // 数组倒序
        self.chatContentArr = (NSMutableArray *)[[self.chatContentArr reverseObjectEnumerator] allObjects];
        [_tableView2 reloadData];
        // 滑到底部
        if (_tableView2.contentSize.height > _tableView2.frame.size.height)
        {
            CGPoint offset = CGPointMake(0, _tableView2.contentSize.height - _tableView2.frame.size.height);
            [_tableView2 setContentOffset:offset animated:YES];
        }

    } fail:^(int code, NSString *msg) {
        NSLog(@"%d, %@", code, msg);
    }];
    
}

- (NSMutableArray *)conversationArr {
    if (_conversationArr == nil) {
        _conversationArr = [[NSMutableArray alloc] init];
    }
    return _conversationArr;
}
- (NSMutableArray *)chatListArr {
    if (_chatListArr == nil) {
        _chatListArr = [[NSMutableArray alloc] init];
    }
    return _chatListArr;
}
- (NSMutableArray *)chatContentArr {
    if (_chatContentArr == nil) {
        _chatContentArr = [[NSMutableArray alloc] init];
    }
    return _chatContentArr;
}

#pragma mark =====通知响应=====
- (void)sendBannedMsg:(NSNotification *)notification {
    NSDictionary *banned = notification.userInfo;
    NSLog(@"--接收到禁言的消息通知--\n%@", banned);
    
    [_msgHandler sendBannedSenderInfo:_liveInfo.senderInfo msg:banned[@"id"]];
}

- (void)sendRemoveAudienceMsg:(NSNotification *)notification {
    NSDictionary *removeAudience = notification.userInfo;
    NSLog(@"--接收到踢人的消息通知--\n%@", removeAudience);
    [_msgHandler sendRemoveAudienceSenderInfo:_liveInfo.senderInfo msg:removeAudience[@"id"]];
	
	
	//不知道为什么踢人没有更新，直接调用一次刷新
	[_audienceTableView refreshAudienceList:nil];
}
- (void)sendSetAdministratorMsg:(NSNotification *)notification {
    NSDictionary *SetAdministrator = notification.userInfo;
    NSLog(@"--接收到设置管理员的消息通知--\n%@", SetAdministrator);
    [_msgHandler sendSetAdministratorSenderInfo:_liveInfo.senderInfo msg:SetAdministrator[@"id"]];
}
- (void)sendDeleteAdministratorMsg:(NSNotification *)notification {
    NSDictionary *DeleteAdministrator = notification.userInfo;
    NSLog(@"--接收到删除管理员的消息通知--\n%@", DeleteAdministrator);
    [_msgHandler sendDeleteAdministratorSenderInfo:_liveInfo.senderInfo msg:DeleteAdministrator[@"id"]];
}
- (void)sendPrivateChatMsg:(NSNotification *)notification {
    NSDictionary *privateChat = notification.userInfo;
    
    _msgView.hidden = NO;
    [self bringSubviewToFront:_msgView];
    
    // 发送私聊消息通知
    NSNotification *not =[NSNotification notificationWithName:@"msgchatVC" object:nil userInfo:privateChat];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:not];
}

- (void)c2cNotification:(NSNotification *)notification {
    NSLog(@"--接收到私聊提醒通知--\n%@", notification.userInfo);
    if (_vChat.isHidden == YES) {
        [_btnPrivateChat setImage:IMAGE(@"live_per_infored") forState:UIControlStateNormal];
    }
    else if (_tableView2.isHidden == YES) {
        [self chatListConversations];
    }
    else
        [self getConversation:_currConversation];
    
}

#pragma mark AudienceListDelegate

//刷新直播间中成员数量（包含头像右侧的全部）
-(void)onFetchGroupMemberList:(int)errCode memberCount:(int)memberCount
{
	/*	现在后台的批量查询用户接口返回的已经是包含目前所有直播间用户的数组，这里不必再次拉取，直接set人数
	[[TIMGroupManager sharedInstance] GetGroupMembers:_liveInfo.groupid succ:^(NSArray *members) {
		
		NSMutableArray *userArr = [[NSMutableArray alloc] init];
		for (TIMGroupMemberInfo *user in members) {
			[userArr addObject:user.member];
			[userArr removeObject:_liveInfo.myuserid];
		}
		NSLog(@"直播有多少成员%ld", userArr.count);
		
		if (_topView && 0 == errCode)
		{
			[_topView setViewerCount:(int)userArr.count likeCount:(int)[_topView getLikeCount]];
		}
		
	} fail:^(int code, NSString *msg) {
		NSLog(@"拉取当前成员失败 %d %@", code, msg);
	}];
	 */
	if (_topView && 0 == errCode)
	{
		[_topView setViewerCount:memberCount likeCount:(int)[_topView getLikeCount]];
	}
}

//调用原来声网的
- (void)doDoubleTapped:(UITapGestureRecognizer *)sender
{
    [self.delegate doDoubleTapped:sender];
}

@end


@implementation TCPushShowResultView
{
    UILabel  *_titleLabel;
    UILabel  *_durationLabel;
    UILabel  *_durationTipLabel;
    UILabel  *_viewerCountLabel;
    UILabel  *_viewerCountTipLabel;
    UILabel  *_praiseLabel;
    UILabel  *_praiseTipLabel;
    UIButton *_backBtn;
    
    ShowResultComplete _backHomepage;
    TCShowLiveTopView *_resultData;
}

- (instancetype)initWithFrame:(CGRect)frame resultData:(TCShowLiveTopView *)resultData backHomepage:(ShowResultComplete)backHomepage {
    if (self = [super initWithFrame:frame]) {
        _resultData = resultData;
        _backHomepage = backHomepage;
        
        [self initUI];
        [_backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)initUI {
    int duration = (int)[_resultData getLiveDuration];
    int hour = duration / 3600;
    int min = (duration - hour * 3600) / 60;
    int sec = duration - hour * 3600 - min * 60;

    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont boldSystemFontOfSize:24];
    [_titleLabel setTextColor:[UIColor colorWithRed:10/255.0 green:204/255.0 blue:172/255.0 alpha:1]];
    [_titleLabel setText:@"直播结束啦!"];
    [self addSubview:_titleLabel];
    
    
    _durationLabel = [[UILabel alloc] init];
    _durationLabel.textAlignment = NSTextAlignmentCenter;
    _durationLabel.font = [UIFont boldSystemFontOfSize:20];
    _durationLabel.textColor = [UIColor whiteColor];
    [_durationLabel setText:[NSString stringWithFormat:@"%02d:%02d:%02d", hour, min, sec]];
    [self addSubview:_durationLabel];
    
    _durationTipLabel = [[UILabel alloc] init];
    _durationTipLabel.textAlignment = NSTextAlignmentCenter;
    _durationTipLabel.font = [UIFont boldSystemFontOfSize:14];
    _durationTipLabel.textColor = [UIColor whiteColor];
    [_durationTipLabel setText:[NSString stringWithFormat:@"直播时长"]];
    [self addSubview:_durationTipLabel];
    
    
    _viewerCountLabel = [[UILabel alloc] init];
    _viewerCountLabel.textAlignment = NSTextAlignmentCenter;
    _viewerCountLabel.font = [UIFont boldSystemFontOfSize:20];
    _viewerCountLabel.textColor = [UIColor whiteColor];
    [_viewerCountLabel setText:[NSString stringWithFormat:@"%ld", [_resultData getTotalViewerCount]]];
    [self addSubview:_viewerCountLabel];
    
    _viewerCountTipLabel = [[UILabel alloc] init];
    _viewerCountTipLabel.textAlignment = NSTextAlignmentCenter;
    _viewerCountTipLabel.font = [UIFont boldSystemFontOfSize:14];
    _viewerCountTipLabel.textColor = [UIColor whiteColor];
    [_viewerCountTipLabel setText:[NSString stringWithFormat:@"观看人数"]];
    [self addSubview:_viewerCountTipLabel];
    
    
    _praiseLabel = [[UILabel alloc] init];
    _praiseLabel.textAlignment = NSTextAlignmentCenter;
    _praiseLabel.font = [UIFont boldSystemFontOfSize:20];
    _praiseLabel.textColor = [UIColor whiteColor];
    [_praiseLabel setText:[NSString stringWithFormat:@"%ld\n", [_resultData getLikeCount]]];
    [self addSubview:_praiseLabel];
    
    _praiseTipLabel = [[UILabel alloc] init];
    _praiseTipLabel.textAlignment = NSTextAlignmentCenter;
    _praiseTipLabel.font = [UIFont boldSystemFontOfSize:14];
    _praiseTipLabel.textColor = [UIColor whiteColor];
    [_praiseTipLabel setText:[NSString stringWithFormat:@"获赞数量"]];
    [self addSubview:_praiseTipLabel];
    
    
    _backBtn = [[UIButton alloc] init];
    [_backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_to_main"] forState:UIControlStateNormal];
    _backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_backBtn setTitle:@"返回首页" forState:UIControlStateNormal];
    [_backBtn setTitleColor:[UIColor colorWithRed:10/255.0 green:204/255.0 blue:172/255.0 alpha:1] forState:UIControlStateNormal];
    [self addSubview:_backBtn];
    
    [self relayout];
}

- (void)relayout {
    CGRect rect = self.bounds;
    
    [_titleLabel sizeWith:CGSizeMake(rect.size.width, 24)];
    [_titleLabel alignParentTopWithMargin:125];
    
    [_durationLabel sizeWith:CGSizeMake(rect.size.width, 15)];
    [_durationLabel layoutBelow:_titleLabel margin:55];
    [_durationTipLabel sizeWith:CGSizeMake(rect.size.width, 14)];
    [_durationTipLabel layoutBelow:_durationLabel margin:7];
    
    [_viewerCountLabel sizeWith:CGSizeMake(rect.size.width, 15)];
    [_viewerCountLabel layoutBelow:_durationTipLabel margin:35];
    [_viewerCountTipLabel sizeWith:CGSizeMake(rect.size.width, 14)];
    [_viewerCountTipLabel layoutBelow:_viewerCountLabel margin:7];
    
    [_praiseLabel sizeWith:CGSizeMake(rect.size.width, 15)];
    [_praiseLabel layoutBelow:_viewerCountTipLabel margin:35];
    [_praiseTipLabel sizeWith:CGSizeMake(rect.size.width, 14)];
    [_praiseTipLabel layoutBelow:_praiseLabel margin:7];
    
    [_backBtn sizeWith:CGSizeMake(225, 35)];
    [_backBtn layoutParentHorizontalCenter];
    [_backBtn layoutBelow:_praiseTipLabel margin:52.5];
    
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.9]];
}

- (void)clickBackBtn {
    _backHomepage();
}



@end
